package com.example.yiqiuser.controller;


import com.example.yiqiuser.entity.Role;
import com.example.yiqiuser.entity.User;
import com.example.yiqiuser.service.RoleService;
import com.example.yiqiuser.util.Page;
import com.example.yiqiuser.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @create by: 周文达 (3220083246@qq.com)
 * @description:
 * @create time: 2023/4/12 22:34
 * @return
 */
@RestController //添加注解 此注解相当于@Controller + @ResponseBody 下方方法全部返回的是@responseBody
@RequestMapping("role")
public class RoleController extends BaseController {

    //引入服务层实现类 注入依赖
    @Resource
    private RoleService roleService;

    @GetMapping("queryAllRole")
    public Result queryAllRole(){
        List<Role> stringList = roleService.queryAll();

        return success(stringList);
    }


    /**
     * 带参数分页查询
     * @param page
     * @return
     */
    @PutMapping("queryByParam")
    public Result queryByParam(@RequestBody Page<Role> page){

      /*  System.out.println("----控制层 查询-----------"+roleService.queryByParam(page).toString());
        System.out.println("=============");
        System.out.println("----控制层 查询-----------"+roleService.queryByParam(page).getList());
*/
        return success(roleService.queryByParam(page));
    }


    /**
     * 根据编号查询
     * @param roleId
     * @return
     */
    @GetMapping("queryById")
    public Result queryById( Integer roleId){

        //调用继承BaseController的方法进行统一的封装使用
        return success(roleService.queryById(roleId));
    }

    /**
     * 添加
     * @param role
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Role role){


        System.out.println("-------控制层 添加---------"+role.getDataScope());
        //调用继承BaseController的方法进行统一的封装使用
        return success(roleService.add(role));
    }


    /**
     * 更新
     * @param role
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Role role){

        System.out.println("-------控制层更新---------"+role);
        //调用继承BaseController的方法进行统一的封装使用
        return success(roleService.update(role));

    }

    /**
     * 删除
     * @param roleId
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer roleId){


        //根据roleId 删除角色之前，先调用服务层接口删除该角色绑定的权限ID
        int sucNum = roleService.deleteMenuByRoleId(roleId);
        if (sucNum>0){
            System.out.println("---------------------根据角色Id删除所拥有的权限成功！");
        }else {
            System.out.println("---------------------根据角色Id删除所拥有的权限失败！！！");
        }
        //调用继承BaseController的方法进行统一的封装使用
        return success(roleService.deleteById(roleId));
    }


    /**
     * 查询所有角色
     * @return
     */
    @PostMapping("queryAll")
    public Result queryAll() {
        return success(roleService.queryAll());
    }

    /**
     * 查询用户的角色
     * @param user
     * @return
     */
    @PostMapping("queryRole")
    public Result queryRole(@RequestBody User user) {
        return success(roleService.queryRole(user));
    }


    /**
     * 查询用户的角色的ID
     * @param user
     * @return
     @PostMapping("addRole")
     public Result addRole(@RequestBody User user) {
     return success(roleService.queryRole(user));
     }*/

    /**
     * 更新用户角色
     * @param user
     * @return
     */
    @PostMapping("updateRole")
    public Result updateRole(@RequestBody User user) {
        int userId= user.getUserId();
        ArrayList roleIds= user.getRoleIds();
        //System.out.println(userId);
        //System.out.println(roleIds);
        return success(roleService.updateRole(userId, roleIds));
    }
    @GetMapping("queryMenuId")
    public Result queryMenuId(Integer roleId){
        return success(roleService.queryMenuId(roleId));
    }

    @PostMapping("addPosAndMenu")
    public Result addPosAndMenu(Integer roleId,String menuIds){
        return success(roleService.addPosAndMenu(roleId,menuIds));
    }



    /**
     * 查询角色权限关联表  根据角色ID查询该角色关联的所有权限ID集合
     * @param roleId
     * @return
     */

    @GetMapping("queryMenuIdListByRoleId")
    public Result queryMenuIdListByRoleId(Integer roleId){
        return success(roleService.queryMenuIdListByRoleId(roleId));
    }


    /**
     * 添加职位（角色）和权限的关联
     * @param roleId
     * @param menuId
     * @return
     */

    @PostMapping("addRoleAndMenu")
    public Result addRoleAndMenu(int roleId, String menuId){
        return success(roleService.addRoleAndMenu(roleId,menuId));
    }



}
